package cn.linstudy.service.impl;

import cn.linstudy.dao.DepartmentMapper;
import cn.linstudy.domain.Department;
import cn.linstudy.qo.DepartmentQuertObject;
import cn.linstudy.qo.PageResult;
import cn.linstudy.result.ResponseResult;
import cn.linstudy.service.DepartmentService;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author XiaoLin
 * @Date 2021/3/2 16:41
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

  @Autowired
  DepartmentMapper departmentMapper;


  @Override
  public ResponseResult add(Department department) {
    departmentMapper.insert(department);
    return new ResponseResult(200,"查询成功");
  }

  @Override
  public ResponseResult delete(Long id) {
    departmentMapper.deleteByPrimaryKey(id);
    return new ResponseResult(200,"删除成功");
  }

  @Override
  public ResponseResult update(Department department) {
    departmentMapper.updateByPrimaryKeySelective(department);
    return new ResponseResult(200,"编辑成功");
  }

  @Override
  public ResponseResult listAll() {
    return new ResponseResult(200,"查询所有成功", departmentMapper.listAll());
  }

  @Override
  public ResponseResult listOneById(Long id) {
    return new ResponseResult(200,"根据ID查询成功",departmentMapper.selectByPrimaryKey(id));
  }

  @Override
  public PageResult<Department> listForPage(DepartmentQuertObject qo) {
    int count = departmentMapper.listCount(qo);
    if (count != 0){
      List<Department> departments = departmentMapper.listForPage(qo);
      return new PageResult<Department>(qo.getCurrentPage(),qo.getPageSize(),count,departments);
    }else {
      return new PageResult<Department>(qo.getCurrentPage(),qo.getPageSize(),count, Collections.emptyList());
    }
  }
}
